<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/libs/modernizr.custom.js"></script>
<script type="text/javascript" src="js/libs/modernizr-srfbet.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="js/libs/jquery.cookie.js"></script>
<script type="text/javascript" src="js/util/Jquery.JSON.js"></script>
<script type="text/javascript" src="js/model/ArrayList.js"></script>
<script type="text/javascript" src="js/dao/parsers/IParser.js"></script>
<script type="text/javascript" src="js/factories/DaoFactory.js"></script>
<script type="text/javascript" src="js/factories/ParserFactory.js"></script>
<script type="text/javascript" src="js/dao/IDAO.js"></script>
<script type="text/javascript" src="js/events/Events.js"></script>
<script type="text/javascript" src="js/view/Main.js"></script>
<script type="text/javascript">
var head = {ready:function(arg){head.callbacks.push(arg);}, callbacks:[]};
var testCount = 2;
if( !Modernizr.supporsaccessors ){
	alert('Our beta site does not support your browser. Upgrade to one of the following:  Internet Explorer 9+, FireFox 2+, Safari 3+, Chrome 1+, and Opera 9.5+ ');
}
    Modernizr.load([
					{
					    test : Modernizr.supporsaccessors,
					    yep: ['js/util/BindingUtils.js',
							    'js/model/User.js',
							    'js/model/Role.js',
							    'js/model/Security.js',
							    'js/model/State.js',
							    'js/model/Domain.js',
							    'js/command/Commands.js',
							    'js/control/Controller.js',
							    'js/control/ServiceLocator.js',
							    'js/dao/parsers/V1Parser.js' ],
					    nope :[  ],
					   	complete: function () {
					       if ( !window.jQuery ) {
					             Modernizr.load('js/libs/jquery.js');
					       }else{
					    	   testCount -= 1;
					    	   if( testCount == 0 ){
						            $.each(head.callbacks, function( index, value ) {
						           	 value();
						            });
					    	   }
					       }
					     }
					  },
                    {
                        test : Modernizr.localstorage,
                        yep: ['js/model/Config.js','js/model/ModelLocator.js', 'js/dao/SrfBetDAO.js' ],
                        nope :[ 'js/model/ConfigNoStorage.js','js/model/ModelLocator.js', 'js/dao/SrfBetDAO.js', 'js/dao/srfBetDAO-noLocalStorage.js' ],
                       	complete: function () {
                           if ( !window.jQuery ) {
                                 Modernizr.load('js/libs/jquery.js');
                           }else{
                        	   testCount -= 1;
					    	   if( testCount == 0 ){
						            $.each(head.callbacks, function( index, value ) {
						           	 value();
						            });
					    	   }
                           }
                         }
                      }
                  ]);
function init(){
	Main.init();
	/************* Concrete Subject *************/
	var model = ModelLocator.getInstance();
	/**************** Observers ****************/
  $('<input type="checkbox" id="Obs1" value="Obs1clicked"/>').insertAfter('body');
  $('<input type="input" id="cbvalue" value="false"/>').insertAfter('#Obs1');
  $('<input type="input" id="modeluser" value=""/>').insertAfter('#cbvalue');
  $('<input type="button" id="fbLoginButton" value="Login with Facebook"/>').insertAfter('#cbvalue').click( function(){
	  window.location = 'https://www.facebook.com/dialog/oauth?client_id=181812358563578&redirect_uri=http://localhost/srfBet/loginTest.html&response_type=token&scope=email';
	  } );
  $('<input type="button" id="logOutButton" value="Logout"/>').insertAfter('#fbLoginButton').click( function(){
	  console.log( ModelLocator.getInstance().state.user );
	  $(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.LOGOUT_USER, {data:ModelLocator.getInstance().state.user} ) );
	  } );
	CheckBinder = function(){
		this.checked;
		this.showLoginButton = function( value ){
			if( value ){
				//make sure to pass the value of 0 or it won't work in Safari or Chrome
				$('#fbLoginButton').show(0);
				$('#logOutButton').hide(0);
			}else{
				//make sure to pass the value of 0 or it won't work in Safari or Chrome
				$('#fbLoginButton').hide(0);
				$('#logOutButton').show(0);
			}
		}
		this.alertRoles = function( value ){
			var output = '';
			if( value == null || value.length == 0  ){
				return;
			}
			for( role in value ){
				output += role.name;
			}
			alert( output );
		}
	};
	var obsCheck1 = new CheckBinder();
	//sample of how to bind to ui
	//remember to bind to val, not value because $('#cbvalue') returns JQuery not input
	//console.log(model.state.user);
	BindingUtils.bind( model.state, "error", $('#cbvalue'), "val" );
	BindingUtils.bind( model.state.user, "fbToken", $('#modeluser'), "val" );
	//model.state.user.showFBLoginButton
	//https://www.facebook.com/dialog/oauth?client_id=181812358563578&redirect_uri=http://localhost/srfBet/api.php&response_type=token
	//sample of how to bind to object
	BindingUtils.bind( model.state, "error", obsCheck1, "checked" );
	BindingUtils.bind( model.state, "showFBLoginButton", obsCheck1, "showLoginButton" );
	BindingUtils.bind( model.security, "roles", obsCheck1, "alertRoles" );
	
	$(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.LOCAL_LOGIN_USER, {data:null} ) );
	
	function handleClick( ){
		$(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.TEST_TYPE, {data:$('#Obs1').is(':checked')} ) );
	}
	
	function addRoleBtnClick(){
		var role = new Role();
		role.name = $('#awsRoleName').val();
		role.keys = $('#awsRoleKeys').val().split(',');
		$(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.UPDATE_ROLE, {data:role} ) );
	}

	function addDomainsBtnClick(){
		var domain = new Domain();
		domain.name = $('#awsDomainName').val();
		$(eventDispatcher).trigger( new SrfBetEvent( SrfBetEvent.UPDATE_DOMAIN, {data:domain} ) );
	}
	
	$('#Obs1').click( handleClick );
	$('#awsAddRoleBtn').click( addRoleBtnClick );
	$('#awsAddDomainBtn').click( addDomainsBtnClick );
}
head.callbacks.push( init );
</script>  
<body>
	<h1>Add Domain to AWS</h1>
	<form id="awsDomanForm">
		<p>Domain Name: <input type="text" id="awsDomainName"/></p>
		<input type="button" value="add domain" id="awsAddDomainBtn"/>
	</form>
	<h1>Add Role to AWS</h1>
	<form id="awsRoleForm">
		<p>Role Name: <input type="text" id="awsRoleName"/></p>
		<p><input type="text" id="awsRoleKeys"/></p>
		<input type="button" value="add roles" id="awsAddRoleBtn"/>
	</form>
</body>
</html>